package com.sifeng.test;

import com.sifeng.enums.FileTypeEnum;
import com.sifeng.utils.MyUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 创建体温登记表
 * @author yang_gsh
 */
public class CreateTemperatureRegister {
    private XSSFWorkbook readExcel(String path) throws IOException {
        InputStream iStream = new FileInputStream(path);
        return new XSSFWorkbook(iStream);
    }

    private void writeExcel(String path, XSSFWorkbook xssfWorkbook, String[] location, String[] values) throws IOException {
        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
        if (sheet == null) {
            System.out.println(path + " is null!!");
            return;
        }

        putValue(location, values, sheet);

        FileOutputStream fileOutputStream = new FileOutputStream(path);

        xssfWorkbook.write(fileOutputStream);
        fileOutputStream.flush();
        xssfWorkbook.close();
    }

    private void putValue(String[] location, String[] values, XSSFSheet sheet) {
        for (int i = 0; i < location.length; i++) {
            String[] locations = location[i].split("-");
            XSSFRow row = sheet.getRow(Integer.parseInt(locations[0]) - 1);
            XSSFCell cell = row.getCell(Integer.parseInt(locations[1]) - 1);
            cell.setCellValue(values[i]);
        }
    }

    public void writeTemperatureValue() {
        String str = "杨可馨-36.7-36.5\n" +
                "蒋雨艳-36.2-36.5\n" +
                "梁肖梨-36.2-36.1\n" +
                "周永军-36.4-36.5\n" +
                "莫慧怡-36.3-36.2\n" +
                "周雅琳-37-37\n" +
                "蒋俊浩-36.3-36.5\n" +
                "蒋荣幸-36.3-36.6\n" +
                "杨轩-36.6-36.2\n" +
                "周丹婷-36.5-36.3\n" +
                "杨君-36.4-36.6\n" +
                "蒋荣转-36.6-36.7\n" +
                "梁贵威-36.6-36.3\n" +
                "蒋华龙-36.6-36.8\n" +
                "周世腾-36.5-36.8\n" +
                "梁贵植-36.5-36.9\n" +
                "杨灿国-36.5-36.7\n" +
                "周嘉芯-36.8-36.5\n" +
                "黎澜-36-36.1\n" +
                "周世农-36.4-36.2\n" +
                "梁荣宵-36.5-36.9\n" +
                "杨明晋-36.5-36.5\n" +
                "钟良琦-36.4-36.6\n" +
                "周敏-36.5-36.7\n" +
                "李富信-36.5-36.6\n" +
                "周贵龙-36.3-36.6\n" +
                "蒋明悦-36.2-36.5";
        String[] location = {"6-4", "7-4", "6-5", "6-6"};
        String basePath = "\\\\TNAS-000D95\\public\\otherProject\\体温表\\四（2）班附件1\\四（2）班附件1\\";
        String[] nameList = str.split("\n");
        for (String names : nameList) {
            String[] strings = names.split("-");
            String excelPath = basePath + strings[0] + "-居家观察信息登记表.xlsx";

            XSSFWorkbook xssfWorkbook = null;
            try {
                xssfWorkbook = readExcel(excelPath);
            } catch (IOException e) {
                System.out.println(strings[0] + " not found...");
                e.printStackTrace();
                continue;
            }

            String[] values = {"早：" + strings[1], "晚：" + strings[2], "无", "是"};
            try {
                writeExcel(excelPath, xssfWorkbook, location, values);
            } catch (IOException e) {
                System.out.println(strings[0] + " write error...");
                e.printStackTrace();
                continue;
            }
            System.out.println(strings[0] + " complete...");
        }
    }

    public void writeTemplate() throws IOException {
        String basePath = "\\\\TNAS-000D95\\public\\otherProject\\体温表\\四（2）班附件1\\四（2）班附件1\\";
        String template = "\\\\Desktop-home\\e\\NAS\\otherProject\\体温表\\四（2）班附件1\\蒋华龙-居家观察信息登记表.xlsx";
        String[] location = {"3-6"};
        List<String> directoryList = MyUtils.readDirectory(basePath, FileTypeEnum.FILE);
        for (String directory : directoryList) {
            XSSFWorkbook sheets = readExcel(template);
            String[] names = directory.split("-");
            String[] values = {names[0]};
            writeExcel(basePath + directory, sheets, location, values);
            sheets.close();
            System.out.println(directory + " complete...");
        }
    }

    public static void main(String[] args) {
        CreateTemperatureRegister createTemperatureRegister = new CreateTemperatureRegister();

        /*try {
            createTemperatureRegister.writeTemplate();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        createTemperatureRegister.writeTemperatureValue();
    }
}
